package com.ego.dubbo.service.impl;

import com.ego.commons.exception.DaoException;
import com.ego.commons.pojo.EasyUITree;
import com.ego.dubbo.service.TbContentCategoryDubboService;
import com.ego.mapper.TbContentCategoryMapper;
import com.ego.pojo.TbContent;
import com.ego.pojo.TbContentCategory;
import com.ego.pojo.TbContentCategoryExample;
import com.ego.pojo.TbContentExample;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title
 * @Author Lyg
 * @Description:
 */
@DubboService
public class TbContentCategoryDubboServiceImpl implements TbContentCategoryDubboService {
    @Autowired
    private TbContentCategoryMapper tbContentCategoryMapper;
    @Override
    public List<EasyUITree> selectByPid(long parentId) {
        TbContentCategoryExample tbContentCategoryExample = new TbContentCategoryExample();
        tbContentCategoryExample.createCriteria().andParentIdEqualTo(parentId);

        List<TbContentCategory> tbContentCategories = tbContentCategoryMapper.selectByExample(tbContentCategoryExample);
        List<EasyUITree> easyUITrees=new ArrayList<>();
        for (TbContentCategory tbContentCategory : tbContentCategories) {
            EasyUITree easyUITree = new EasyUITree();
            easyUITree.setId(tbContentCategory.getId());
            easyUITree.setText(tbContentCategory.getName());
            easyUITree.setState(tbContentCategory.getIsParent()?"closed":"open");
            easyUITrees.add(easyUITree);

        }
        return easyUITrees;
    }

    @Override
    @Transactional(rollbackFor =Exception.class)
    public int insertContentCategory(TbContentCategory contentCategory) throws DaoException {

        TbContentCategoryExample tbContentCategoryExample = new TbContentCategoryExample();
        tbContentCategoryExample.createCriteria().andNameEqualTo(contentCategory.getName());

            List<TbContentCategory> tbContentCategories = tbContentCategoryMapper.selectByExample(tbContentCategoryExample);
        System.out.println("tbContentCategories = " + tbContentCategories);

            if (tbContentCategories.size()==0){
                try {
                    int i = tbContentCategoryMapper.insertSelective(contentCategory);
                    if (i==1){
                        TbContentCategory tbContentCategory = new TbContentCategory();
                        tbContentCategory.setId(contentCategory.getParentId());
                        tbContentCategory.setUpdated(contentCategory.getCreated());
                        tbContentCategory.setIsParent(true);

                        tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory);

                        return 1;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new DaoException("添加失败");
                }
            }
                return 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateName(TbContentCategory tbContentCategory) throws DaoException {
        TbContentCategoryExample tbContentCategoryExample = new TbContentCategoryExample();
        tbContentCategoryExample.createCriteria().andNameEqualTo(tbContentCategory.getName());

        List<TbContentCategory> tbContentCategories = tbContentCategoryMapper.selectByExample(tbContentCategoryExample);

        if (tbContentCategories.size()>0){
            return 0;
        }else {
            try {
                return tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory);
            } catch (Exception e) {
                e.printStackTrace();
                throw new DaoException("重命名失败");
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteById(long id) throws DaoException {
        TbContentCategory tbContentCategory = tbContentCategoryMapper.selectByPrimaryKey(id);
        if (tbContentCategory.getIsParent()){
            return 0;
        }else {
            try {
                int i = tbContentCategoryMapper.deleteByPrimaryKey(id);
                if (i==1){
                    TbContentCategoryExample tbContentCategoryExample = new TbContentCategoryExample();
                    tbContentCategoryExample.createCriteria().andParentIdEqualTo(tbContentCategory.getParentId());

                    List<TbContentCategory> tbContentCategories = tbContentCategoryMapper.selectByExample(tbContentCategoryExample);

                    if (tbContentCategories.size()==0){
                        TbContentCategory tbContentCategory1 = new TbContentCategory();
                        tbContentCategory1.setId(tbContentCategory.getParentId());
                        tbContentCategory1.setIsParent(false);
                        int i1 = tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory1);
                        if (i1==1){
                            return 1;
                        }else {
                            return 0;
                        }
                    }
                    return 1;
                }
                return 0;
            } catch (Exception e) {
                e.printStackTrace();
                throw new DaoException("删除失败");
            }
        }
    }

}
